﻿using System;
using System.Collections.Generic;
using System.Text;
using eBay.Service.Core.Soap;
using ebay.ebayApi;
using ebay.Dal;

namespace ebay.BLL
{
    public class Category
    {
        #region 通过ebay Api获取类别档案
        public List<ebay.Model.Category> GetCategory()
        {
            EbayAccount account = new EbayAccount();

            string token = account.GetToken();
            if (string.IsNullOrEmpty(token))
            {
                throw new Exception("数据库中还没有账户信息");
            }
            ebayCallsApi apicall = new ebayCallsApi(token);
            CategoryTypeCollection cats = apicall.GetCategory();
            List<ebay.Model.Category> list = new List<ebay.Model.Category>();
            foreach (CategoryType category in cats)
            {
                if (string.IsNullOrEmpty(category.CategoryName))
                    continue;
                ebay.Model.Category m = new ebay.Model.Category();
                m.CategoryId = category.CategoryID;
                m.CategoryLevel = category.CategoryLevel;
                m.CategoryName = category.CategoryName;
                if (category.CategoryParentID != null)
                {
                    foreach (string s in category.CategoryParentID)
                    {
                        m.CategoryParentID += s + ">>";
                    }
                    if (!string.IsNullOrEmpty(m.CategoryParentID))
                    {
                        m.CategoryParentID = m.CategoryParentID.Substring(0, m.CategoryParentID.Length - 2);
                    }

                    foreach (string s in category.CategoryParentName)
                    {
                        m.CategoryParentName += s + ">>";
                    }
                    if (!string.IsNullOrEmpty(m.CategoryParentName))
                    {
                        m.CategoryParentName = m.CategoryParentName.Substring(0, m.CategoryParentName.Length - 2);
                    }
                }
                m.CategoryTypeId = 1;
                list.Add(m);
            }
            return list;

        }
        #endregion

        #region 通过ebay Api获取类别2档案
        public List<ebay.Model.Category> GetCategory2CS()
        {
            EbayAccount account = new EbayAccount();

            string token = account.GetToken();
            if (string.IsNullOrEmpty(token))
            {
                throw new Exception("数据库中还没有账户信息");
            }
            ebayCallsApi apicall = new ebayCallsApi(token);
            CategoryTypeCollection cats = apicall.GetCategory2CS();
            List<ebay.Model.Category> list = new List<ebay.Model.Category>();
            foreach (CategoryType category in cats)
            {
                bool hasProdFinder = category.ProductFinderIDs.Count > 0;
                if (category.CatalogEnabled || hasProdFinder
                    || category.ProductSearchPageAvailable
                    || category.ProductSearchPageAvailableSpecified)
                {
                    ebay.Model.Category m = new ebay.Model.Category();
                    m.CategoryId = category.CategoryID;
                    m.CategoryLevel = category.CategoryLevel;
                    m.CategoryName = category.CharacteristicsSets[0].Name;
                    m.CategoryParentID = category.CharacteristicsSets[0].AttributeSetID.ToString();
                    m.CategoryParentName = "";
                    m.CategoryTypeId = 2;
                    list.Add(m);
                }

            }
            return list;
        }
        #endregion
    }
}
